package com.neuedu.controller;

import com.neuedu.common.Const;
import com.neuedu.common.JSONData;
import com.neuedu.pojo.UserInfo;
import com.neuedu.service.CartService;
import com.neuedu.vo.CartVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
@Controller
@RequestMapping("/cart/")
@Api(value = "购物车相关接口",description = "购物车相关接口")
public class CartController {
    @Autowired
    private CartService cartService;
    /**
     * productId:==>productSKUId
     * 1、考虑问题：count：
     *      商品的数量是否超出了 数据库的 商品库存
     *      此项目：校验 放到前端校验
     *
     */
    @ApiOperation(value = "购物车添加商品",notes = "购物车添加商品",httpMethod = "POST")
    @RequestMapping(value = "addProduct",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<CartVo> addProduct(HttpSession session, Integer productId, Integer count){
        //1、判断用户是否登录
        UserInfo userInfo = (UserInfo) session.getAttribute(Const.CURRENT_USERINFO);
        if(userInfo==null){
            return JSONData.buildError("您未登录，请先登录");
        }
        //2、购物添加
        JSONData<CartVo> data = cartService.addProduct(productId, count, userInfo.getId());
        return data;
    }
    @ApiOperation(value = "查询购物车所有商品",notes = "查询购物车所有商品",httpMethod = "POST")
    @RequestMapping(value = "queryCartAllInfoByUserId",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<CartVo> queryCartAllInfoByUserId(HttpSession session){
        //1、判断用户是否登录
        UserInfo userInfo = (UserInfo) session.getAttribute(Const.CURRENT_USERINFO);
        if(userInfo==null){
            return JSONData.buildError("您未登录，请先登录");
        }
        //2、查询所用的信息
        CartVo data = cartService.queryCartAllInfoByUserId(userInfo.getId());
        return JSONData.buildSuccess(data);
    }

    /**
     * 修改购物车 商品的数量 ： 手动、 + -
     */
    @ApiOperation(value = "修改购物车商品数量",notes = "修改购物车商品数量",httpMethod = "POST")
    @RequestMapping(value = "updateCartProductNum",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<CartVo> updateCartProductNum(HttpSession session,Integer productId,Integer count){
        //1、判断用户是否登录
        UserInfo userInfo = (UserInfo) session.getAttribute(Const.CURRENT_USERINFO);
        if(userInfo==null){
            return JSONData.buildError("您未登录，请先登录");
        }
        JSONData<CartVo> data = cartService.updateCartProductNum(userInfo.getId(), count, productId);
        return data;
    }
    @ApiOperation(value = "获取购物车所有商品数量",notes = "获取购物车所有商品数量",httpMethod = "POST")
    @RequestMapping(value = "getCartProductCount",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<Integer> getCartProductCount(HttpSession session){
        //1、判断用户是否登录
        UserInfo userInfo = (UserInfo) session.getAttribute(Const.CURRENT_USERINFO);
        if(userInfo==null){
            return JSONData.buildSuccess(0);
        }
        JSONData<Integer> data = cartService.selCartProductCount(userInfo.getId());
        return data;
    }
    /**
     * 删除购物车商品： 批量删除
     *      如果是批量删，则与前端进行约定：(多个使用逗号来分隔)  productId,productId,productId.....
     *
     */
    @ApiOperation(value = "删除购物车中指定商品",notes = "删除购物车中指定商品",httpMethod = "POST")
    @RequestMapping(value = "delProduct",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<CartVo> delProduct(HttpSession session,String productIds){
        //1、判断用户是否登录
        UserInfo userInfo = (UserInfo) session.getAttribute(Const.CURRENT_USERINFO);
        if(userInfo==null){
            return JSONData.buildError("您未登录，请先登录");
        }
        JSONData<CartVo> data = cartService.delProduct(userInfo.getId(), productIds);
        return data;
    }
}
